package com.gmail.jaredjeya.awesomeplatformer.view;

import com.gmail.jaredjeya.awesomeplatformer.model.sprites.Sprite;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.view.SurfaceHolder;

public class RenderThread extends Thread {

	protected SurfaceHolder holder;
	protected Context context;
	protected Display display;
	protected Handler handler;
	protected Paint paint;
	private boolean isSetup = false, running = false;;
	public static int fpsDraw;

	public RenderThread(SurfaceHolder holder, Context context, Display display, Handler handler) {
		super();
		this.holder = holder;
		this.context = context;
		this.display = display;
		this.handler = handler;
		this.paint = new Paint();
	}

	@Override
	public void run() {
		int fps = 0;
		long timer = System.currentTimeMillis();
		running = true;
		while (running) {
			Canvas canvas = holder.lockCanvas();
			// DO NOT ACCESS CANVAS ABOVE THIS LINE

			if (!isSetup) {
				isSetup = true;
				Sprite.load(context);
				display.setup(canvas, paint);
			}
			
			if (System.currentTimeMillis() - timer >= 1000) {
				fpsDraw = fps;
				fps = 0;
				timer = System.currentTimeMillis();
			}
			display.draw(canvas, paint);
			fps++;

			// DO NOT ACCESS CANVAS BELOW THIS LINE

			holder.unlockCanvasAndPost(canvas);
		}
	}

}
